<?php

namespace App\Models\Reservation;

use App\Models\Hospital;
use App\Models\Patient;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use App\Project\Traits\Model\HasReception;
use Illuminate\Database\Query\Builder as QueryBuilder;

class Pool extends Model
{
    use HasReception;

    protected $table = 'st_pools';

    public function patient()
    {
        return $this->belongsTo(Patient::class)->simpleField();
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function reservation()
    {
        return $this->belongsTo(Reservation::class);
    }

    public function hospital()
    {
        return $this->belongsTo(Hospital::class);
    }

    public function reject()
    {
        return $this->hasMany(PoolReject::class);
    }

    public function scopeWhereNotReject(Builder $builder, User $doctor = null)
    {
        $doctor = $doctor ? : \Auth::user();

        return $builder->whereNotIn('st_pools.reservation_id', function (QueryBuilder $query) use ($doctor) {
            $query->select('st_pool_rejects.reservation_id')->from('st_pool_rejects')->where(['st_pool_rejects.doctor_id'=> $doctor->id]);
        });
    }
}
